import React from 'react';
import { inject,observer } from 'mobx-react';
import * as mobx from 'mobx';
import { getAntd,getEcCom,getLabel } from '../../../util/convert';
import './index.less';

@inject('freeFlowStore')
@observer
export default class Main extends React.Component{
    componentDidMount(){
        const { freeFlowStore } = this.props;
        freeFlowStore.doInit();
    }
    componentWillUnmount(){
        const { freeFlowStore } = this.props;
        freeFlowStore.resetStore();
    }
    render(){
        const { Button,Modal } = getAntd();
        const { WeaTableEdit,WeaDialogFooter,WeaRightMenu } = getEcCom();
        const { freeFlowStore} = this.props;
        const { columns,datas } = freeFlowStore;
        return (
            <div className="wf-freeflow">
                <div className="wf-freeflow-content">
                    <WeaRightMenu {...this.getMoreBtns()}>
                        <WeaTableEdit
                            isModalEdit = {false}
                            title = {getLabel(385074,'流转步骤设置')}
                            columns = {mobx.toJS(columns)}
                            datas = {mobx.toJS(datas)}
                            draggable = {true}
                            showCopy = {false}
                            onChange = {this.doChange}
                            onAdd = {this.doAdd}
                        />
                    </WeaRightMenu>
                </div>
                <WeaDialogFooter buttons={this.getButtons()} moreBtn={this.getMoreBtns()} />
            </div>
        )
    }

    doChange = (v) => {
        const { freeFlowStore} = this.props;
        freeFlowStore.setDatas(v);
    }
    doAdd = (keys,rows) => {
        const { freeFlowStore } = this.props;
        freeFlowStore.addStepIndex();
        const curRow = rows[0];
        curRow.stepname = `${getLabel(18731,'步骤')}${freeFlowStore.stepindex}`;
        curRow.signtype = "0";
    }
    getButtons(){
        const { Button,Modal } = getAntd();
        let buttons = [];
        buttons.push(<Button type='primary' onClick={this.doSave.bind(this, false)}>{getLabel(86,'保存')}</Button>);
        //buttons.push(<Button type='primary' onClick={this.doSave.bind(this, true)}>保存并提交流程</Button>);
        return buttons;
    }
    getMoreBtns(){
        let btns = [];
        btns.push({
            key: '0',
            icon:  <i className='icon-coms-Preservation' />,
            content: getLabel(86,'保存'),
            onClick: key => this.doSave(),
        });
        return {datas:btns};
    }
    doSave = (directSubmit) => {
        const { freeFlowStore } = this.props;
        const { datas } = freeFlowStore;
        let hasEmpty = false;
        datas && datas.map(row =>{
            if(row.operators === "")
                hasEmpty = true;
        });
        if(hasEmpty){
            Modal.warning({
                title: getLabel(131329,'信息确认'),
                content: getLabel(384146,'必要信息不完整，红色*为必填项'),
                okText: getLabel(826,`确定`),
                cancelText:getLabel(31129,`取消`),
                onOk() {}
            });
            return;
        }
        freeFlowStore.doSave(directSubmit);
    }
}
